package com.lkd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lkd.dto.RegionDto;
import com.lkd.entity.Region;
import com.lkd.mapper.RegionMapper;
import com.lkd.result.PageBean;
import com.lkd.service.NodeService;
import com.lkd.service.RegionService;
import com.lkd.vo.RegionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RegionServiceImpl implements RegionService {

    @Autowired
    private RegionMapper regionMapper;

    @Autowired
    private NodeService nodeService;

    /**
     * 分页查询 区域管理
     * @param pageIndex
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public PageBean<RegionVo> findByRegionPage(Integer pageIndex, Integer pageSize, String name) {
        PageHelper.startPage(pageIndex,pageSize);
        List<RegionVo> voList = regionMapper.selectByCondition(name);
        Page<RegionVo> page = (Page<RegionVo>) voList;
        return new PageBean(pageIndex, pageSize, (long) page.getPages(), page.getTotal(), page.getResult());
    }

    /**
     * 新增区域
     * @param dto
     * @return
     */
    @Override
    public void save(RegionDto dto) {
//        对象属性拷贝
        Region region = BeanUtil.toBean(dto, Region.class);
        regionMapper.insert(region);
    }

    /**
     * 修改区域
     * @param dto
     * @return
     */
    @Override
    public void update(RegionDto dto) {
//        对象属性拷贝
        Region region = BeanUtil.toBean(dto, Region.class);
        regionMapper.updateById(region);
    }

    @Override
    public RegionVo selectRegionById(String name, String regionId) {
        return regionMapper.selectRegionById(name,regionId);
    }

    @Override
    public RegionVo selectByNodeId(Long nodeId) {
        return regionMapper.selectByNodeId(nodeId);
    }

    @Override
    public RegionVo selectRegionByNodeId(Long nodeId) {
        return regionMapper.selectRegionByNodeId(nodeId);
    }

    @Override
    public RegionVo selectRegionByInnerCode(String innerCode) {
        return regionMapper.selectRegionByInnerCode(innerCode);
    }
}
